class LazyMan {
  constructor(name) {
    this.queue = []
    this.add(() => {
      console.log('我是' + name);
    })
    setTimeout(() => {
      this.next()
    })
  }
  eat(food) {
    this.add(() => console.log('吃吃吃' + food))
    return this
  }
  sleep(time) {
    this.add(async () => {
      await new Promise((resolve) => {
        setTimeout(() => {
          resolve()
          console.log(`睡了${time}s`);
        }, time)
      })
    })
    return this
  }
  drink(thing) {
    this.add(() => console.log('喝喝喝' + thing))
    return this
  }
  sleepFirst(time) {
    this.addFirst(async () => {
      await new Promise((resolve) => {
        setTimeout(() => {
          resolve()
          console.log(`先睡${time}s`);
        }, time)
      })
    })
    return this
  }
  add(task) {
    this.queue.push(async () => {
      await task()
      this.next()
    })
  }
  addFirst(task) {
    this.queue.unshift(async () => {
      await task()
      this.next()
    })
  }
  next() {
    let fn = this.queue.shift()
    if (fn) fn()
  }
}
new LazyMan('煎饼狗子').eat('汉堡').sleep(3000).drink('百事可乐').sleepFirst(2000)